﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace Infrastructure.Crosscutting.Authorize
{
    public class AssemblyPermissionService
    {
        public List<Dto.ControllerInfoListDto> GetAssemblyControllerInfo(Assembly assembly)
        {
            List<Dto.ControllerInfoListDto> controllerList = new List<Dto.ControllerInfoListDto>();
            var currentControllerList = assembly.GetTypes().Where(e => e.FullName.EndsWith("Controller"));
            Dto.ControllerInfoListDto temp = null;
            foreach (var controller in currentControllerList)
            {
                temp = new Dto.ControllerInfoListDto();
                temp.ActionList = new List<string>();
                foreach (var method in controller.GetMethods())
                {
                    if (!temp.ActionList.Contains(method.Name))
                    {
                        //结尾是Result 并且是Public
                        if (Infrastructure.Crosscutting.Comm.Service.MvcResultService.IsMvcActionResult(method.ReturnType) && method.IsPublic)
                        {
                            temp.ContorllerType = controller;
                            temp.ActionList.Add(method.Name);
                        }
                    }
                }
                controllerList.Add(temp);
            }
            return controllerList;
        }

        public List<Dto.ControllerInfoListDto> GetAssemblyControllerInfo(string assemblyFullName)
        {
            Assembly currentAssembly = Assembly.Load(assemblyFullName);
            return GetAssemblyControllerInfo(currentAssembly);
        }

        public List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto> GetAssemblyAllPermission(Assembly assembly)
        {
            var allPermission = new List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto>();
            var currentControllerList = assembly.GetTypes().Where(e => e.FullName.EndsWith("Controller"));
            foreach (var controller in currentControllerList)
            {
                foreach (var method in controller.GetMethods())
                {
                    //结尾是Result 并且是Public
                    if (Infrastructure.Crosscutting.Comm.Service.MvcResultService.IsMvcActionResult(method.ReturnType) && method.IsPublic &&
                        !allPermission.Where(e => e.ControllerName == controller.FullName && e.ActionName == method.Name).Any())
                    {
                        var per = new Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto();
                        per.ActionId = 0;
                        per.ActionName = method.Name;
                        per.ActionRemark = method.Name;
                        per.ControllerClassId = 0;
                        per.ControllerClassName = "";
                        per.ControllerId = 0;
                        per.ControllerName = controller.FullName;
                        per.ControllerRemark = controller.FullName;
                        allPermission.Add(per);
                    }
                }
            }
            return allPermission;
        }

        public List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto> GetAssemblyAllPermission(string assembly)
        {
            Assembly currentAssembly = Assembly.Load(assembly);
            return GetAssemblyAllPermission(currentAssembly);
        }
    }
}